const express = require('express');
const router = express.Router();
const models = require('../models');
const APIResult = require('../util/util').APIResult;
const TeamService = require('../services/team-service');
const roles = require('../constants/constant').roles;

/**
 * @api {get} /webim/team/:storeId 获取店铺的所有团队列表
 * @apiName GetTeams
 * @apiGroup Team
 * @apiVersion 0.1.0
 *
 * @apiParam {String} storeId 店铺id
 *
 * @apiSuccess {Object[]} teams 返回团队列表
 *
 * @apiExample {bash} 示例：
 *      GET /webim/team/SUPER007
 *
 * @apiSuccessExample 成功响应:
 *    HTTP/1.1 200 OK
 *    {
 *      "code": 200,
 *      "result": [
 *        {
 *          "id": 2,
 *          "teamName": "开思客服",
 *          "Users": [
 *            {
 *              "id": "5R2Bv6W9N",
 *              "uid": "zhangjl",
 *              "nickname": "张建临",
 *              "contactNumber": "18688780069",
 *              "storeName": "开思",
 *              "storeId": "SUPER007",
 *              "partyGroup": "张建临",
 *              "portraitUri": "",
 *              "displayName": "张建临",
 *              "isMaster": true
 *            },
 *            {
 *              "id": "QLRCcmWHf",
 *              "uid": "kscxy",
 *              "nickname": "陈星言",
 *              "contactNumber": "18688780069",
 *              "storeName": "开思",
 *              "storeId": "SUPER007",
 *              "partyGroup": "陈星言",
 *              "portraitUri": "",
 *              "displayName": "陈星言",
 *              "isMaster": false
 *            }
 *          ]
 *        },
 *        {
 *          "id": 3,
 *          "teamName": "开思译码",
 *          "Users": [
 *            {
 *              "id": "ME1Gu0U9k",
 *              "uid": "kslwp",
 *              "nickname": "卢卫平",
 *              "contactNumber": "18688780069",
 *              "storeName": "开思",
 *              "storeId": "SUPER007",
 *              "partyGroup": "卢卫平",
 *              "portraitUri": "",
 *              "displayName": "卢卫平",
 *              "isMaster": false
 *            }
 *          ]
 *        }
 *      ]
 *    }
 */
router.get('/:storeId', (req, res, next) => {
  let storeId = req.params.storeId;
  TeamService
    .queryTeamsDetail(storeId)
    .then(results => res.send(new APIResult(200, results)))
    .catch(next);
});


/**
 * @api {get} /webim/team/:storeId/customer_service 获取店铺所有客服团队
 * @apiName GetCustomerServiceTeams
 * @apiGroup Team
 * @apiVersion 0.1.0
 *
 * @apiParam {String} storeId 店铺id
 *
 * @apiSuccess {Object[]} teams 返回团队列表
 *
 * @apiExample {bash} 示例：
 *      GET /webim/team/SUPER007/customer_service
 *
 * @apiSuccessExample 成功响应:
 *    HTTP/1.1 200 OK
 *    {
 *      "code": 200,
 *      "result": [
 *        {
 *          "id": 2,
 *          "teamName": "开思客服",
 *          "Users": [
 *            {
 *              "id": "5R2Bv6W9N",
 *              "uid": "zhangjl",
 *              "nickname": "张建临",
 *              "contactNumber": "18688780069",
 *              "storeName": "开思",
 *              "storeId": "SUPER007",
 *              "partyGroup": "张建临",
 *              "portraitUri": "",
 *              "displayName": "张建临",
 *              "isMaster": true    //是否为该团队主管
 *            },
 *            {
 *              "id": "QLRCcmWHf",
 *              "uid": "kscxy",
 *              "nickname": "陈星言",
 *              "contactNumber": "18688780069",
 *              "storeName": "开思",
 *              "storeId": "SUPER007",
 *              "partyGroup": "陈星言",
 *              "portraitUri": "",
 *              "displayName": "陈星言",
 *              "isMaster": false
 *            }
 *          ]
 *        }
 *      ]
 *    }
 */
router.get('/:storeId/customer_service', (req, res, next) => {
  let storeId = req.params.storeId;
  TeamService
    .queryTeamsDetail(storeId, roles.CUSTOMER_SERVICE_ROLE)
    .then(results => res.send(new APIResult(200, results)))
    .catch(next);
});


module.exports = router;
